<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 内容卡片标签 -->
      <el-card class="card-container">
        <!-- 表单 -->
        <div>
          <el-breadcrumb
            separator-class="el-icon-arrow-right"
            class="breadcrumb"
            v-if="id"
          >
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{ name }}</el-breadcrumb-item>
            <el-breadcrumb-item>标签管理</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <el-form
          :model="searchForm"
          class="tag-head"
          size="mini"
          :inline="true"
        >
          <el-row class="inline-info">
            <el-col :span="16">
              <el-form-item label="标签名称">
                <el-input
                  class="inp-tag"
                  v-model="searchForm.tagName"
                  placeholder="请输入内容"
                ></el-input>
              </el-form-item>

              <el-form-item label="状态">
                <el-select v-model="searchForm.state" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                    filterable
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="clear" plain>清除</el-button>
                <el-button type="primary" @click="search">搜索</el-button>
              </el-form-item>
            </el-col>
            <el-col :span="8">
            <el-form-item class="addbt">
              <el-button
                type="text"
                icon="el-icon-back"
                @click="$router.back()"
                v-if="id"
                >返回学科</el-button
              >
              <el-button
                type="success"
                icon="el-icon-edit"
                @click="addTags"
                size="mini"
                class="el-add-btn"
                >新增标签</el-button
              >
            </el-form-item>

            </el-col>
          </el-row>
        </el-form>

        <!-- 警示框 -->
        <el-alert
          class="alert"
          :title="`数据一共${total}条`"
          type="info"
          :closable="false"
          show-icon
        >
        </el-alert>
        <!-- table标签 -->
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column
            fixed
            type="index"
            :index="indexMethod"
            label="序号"
            width="100"
          >
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="150">
          </el-table-column>
          <el-table-column prop="tagName" label="标签名称" width="150">
          </el-table-column>
          <el-table-column prop="username" label="创建者" width="150">
          </el-table-column>
          <el-table-column prop="addDate" width="200" label="创建日期">
            <template #default="{ row }">
              {{ row.addDate | timeFormate }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态" width="150">
            <template #default="{ row }">
              <div v-if="row.state">已启用</div>
              <div v-else>已禁用</div>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200">
            <template #default="{ row }">
              <el-row class="inline-info">
                <el-col :span="8">
                  <el-button
                    v-if="!row.state"
                    type="text"
                    size="small"
                    @click="editState(row.id, 1)"
                    >启用</el-button
                  >
                </el-col>
                <el-col :span="8">
                  <el-button
                    v-if="row.state"
                    @click="editState(row.id, 0)"
                    type="text"
                    size="small"
                    >禁用</el-button
                  >
                </el-col>

                <el-col :span="8">
                  <el-button
                    :disabled="row.state === 1"
                    type="text"
                    size="small"
                    @click="editClick(row)"
                    >修改</el-button
                  >
                </el-col>
                <el-col :span="8">
                  <el-button
                    :disabled="row.state === 1"
                    type="text"
                    size="small"
                    @click="delClick(row)"
                    >删除</el-button
                  >
                </el-col>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          hide-on-single-page
          class="pagination"
          background
          :page-size="pagesize"
          layout=" prev, pager, next, sizes, jumper"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :total="total"
          style="margin-top: 20px"
        >
        </el-pagination>
      </el-card>
    </div>
    <tags-add
      ref="tagsAdd"
      :subjectNameList="subjectNameList"
      @getList="getList"
      :dialog-visible="dialogFormVisible"
      @close="close"
      :id="id"
    ></tags-add>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects'
import TagsAdd from '../components/tags-add.vue'
// import { timeFormate } from '@/filters'
export default {
  data () {
    return {
      subjectNameList: [],
      // formLabelWidth: '80px',
      submitLoading: false,
      dialogFormVisible: false,
      tableLoading: false,
      input: '',
      subjectName: '',
      id: null,
      name: null,
      tableData: [
        {
          id: '',
          subjectName: '',
          tagName: '',
          username: '',
          addDate: '',
          state: 1
        }
      ],
      searchForm: {
        tagName: '',
        state: null
      },
      options: [
        {
          value: 1,
          label: '启用'
        },
        {
          value: 0,
          label: '禁用'
        }
      ],
      value: '',
      page: 1,
      pagesize: 10,
      total: 0
    }
  },
  // 监听
  watch: {
    $route: {
      handler: function (route) {
        // console.log(route.query)
        // 对route.query的值的检测
        // 如果有 id 和 name 则去取这个字段 如果没有 undefined 也不会报错
        this.id = route.query && route.query.id
        // console.log(route.query.id)
        this.name = route.query && route.query.name
        if (!route.query.id) {
          this.getList()
        }
      },
      // 开启立即监听在初始化的时候就会监听一次
      immediate: true
    }
    // 如果有id的话，发请求赋值，重新渲染数据
  },
  created () {
    this.getList()
  },
  components: {
    TagsAdd
  },

  methods: {
    // 清空搜索
    clear () {
      // 搜索表单置空
      this.searchForm = {
        tagName: '',
        state: null
      }
      // 重新渲染标签列表
      this.getList()
    },
    // 搜索
    async search () {
      console.log(this.searchForm)
      const { data } = await list({
        tagName: this.searchForm.tagName,
        state: this.searchForm.state
      })
      this.tableData = data.items
      this.total = data.counts
    },
    // 获取列表
    async getList (state) {
      this.tableLoading = true
      const { data } = await list({
        page: this.page,
        pagesize: this.pagesize,
        state: this.searchForm.state,
        tagName: this.searchForm.tagName,
        subjectID: this.id
      })
      const { items, counts } = data
      this.tableData = items
      this.total = counts
      this.tableLoading = false
    },
    // 新增标签
    async addTags () {
      this.dialogFormVisible = true
      const { data } = await simple()
      this.subjectNameList = data
    },
    // 修改
    async editClick (row) {
      // 获得子组件的属性和方法，调用子组件的详情页getTagsDetail方法获取当前行的数据
      this.$refs.tagsAdd.getTagsDetail(row)
      this.dialogFormVisible = true
      const { data } = await simple()
      this.subjectNameList = data
    },
    // 删除
    delClick (row) {
      this.$confirm('此操作将永久删除该标签, 是否继续?', '温馨提示', { type: 'warning' })
        .then(async () => {
          await remove({ id: row.id })
          // 分页的删除边界处理
          if (this.page > 1 && this.tableData.length === 1) {
            console.log(this.tableData.length)
            this.page--
          }
          this.$message.success('删除成功！')
          this.getList()
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 每页条数发生改变
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      // 调用接口请求数据
      this.getList()
    },
    // 当前页码发生改变
    handleCurrentChange (val) {
      this.page = val
      // 调用接口请求数据
      this.getList()
    },
    // 更改启用禁用
    async editState (id, state) {
      await changeState({ id, state })
      this.getList()
    },
    // 页码的计算(页码*当前页码条数+索引值+1)
    indexMethod (index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    // 关闭弹窗
    close () {
      this.dialogFormVisible = false
    }
  }
}
</script>

<style scoped>
.pagination {
  margin-left: 500px;
  margin-right: 10px;
}
.breadcrumb {
  padding-left: 20px;
  padding-bottom: 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid rgb(232, 232, 232);
}
::v-deep .el-table tr th {
  background-color: rgb(249, 249, 249);
  border-bottom: 2px solid rgb(232, 232, 232) !important;
}
.inp-tag {
  width: 180px;
}
.card-container {
  margin-top: 20px;
}
.alert {
  margin: 10px 0;
}
.el-add-btn {
  float: right;
}
.addbt {
  float: right;
}
</style>
